package com.javielinux.pruebadata;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.android.dataframework.DataFramework;

public class PruebaDataFramework extends ListActivity {
    private static final int ACTIVITY_CREATE=0;
    private static final int ACTIVITY_EDIT=1;
    
    private static final int INSERT_ID = Menu.FIRST;
    private static final int DELETE_ID = Menu.FIRST + 1;
    private static final int BACKUP_ID = Menu.FIRST + 2;
    private static final int RESTORE_ID = Menu.FIRST + 3;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notes_list);
        
        try {
        	DataFramework.getInstance().open(this, "com.javielinux.pruebadata");
		} catch (Exception e) {
			e.printStackTrace();
		}

		fillData();
        
    }
    
    private void fillData() {
    	Cursor notesCursor = DataFramework.getInstance().getCursor("notes");
        startManagingCursor(notesCursor);
        
        String[] from = new String[] {"title"};
        
        int[] to = new int[]{R.id.text1};
        
        SimpleCursorAdapter notes = 
        	    new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
        setListAdapter(notes);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, INSERT_ID, 0, R.string.menu_insert);
        menu.add(0, DELETE_ID, 0,  R.string.menu_delete);
        menu.add(0, BACKUP_ID, 0,  "Backup");
        menu.add(0, RESTORE_ID, 0,  "Restore");
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
        case INSERT_ID:
            createNote();
            return true;
        case BACKUP_ID:
        	try {
        		DataFramework.getInstance().backup("/sdcard/backup-notes.xml");
        	} catch (Exception e) {
        		
        	}
            return true;
        case RESTORE_ID:
        	try {
        		DataFramework.getInstance().emptyTablesBackup();
        		DataFramework.getInstance().restore("/sdcard/backup-notes.xml");
        		fillData();
        	} catch (Exception e) {
        		
        	}
            return true;
        case DELETE_ID:
        	DataFramework.getInstance().emptyTable("notes");
            fillData();
            return true;
        }
       
        return super.onMenuItemSelected(featureId, item);
    }

    private void createNote() {
        Intent i = new Intent(this, NoteEdit.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, NoteEdit.class);
        i.putExtra(DataFramework.KEY_ID, id);
        startActivityForResult(i, ACTIVITY_EDIT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, 
                                    Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();
    }
    
}